//
//  filterBtnView.m
//  功能模块代码
//
//  Created by apple  on 2017/11/28.
//  Copyright © 2017年 JMZ. All rights reserved.
//

#import "filterBtnView.h"

@interface filterBtnView()
@property(nonatomic,strong)UILabel * titleLabel;
@property(nonatomic,strong)UIButton * selectBtn;
@property(nonatomic,strong)NSString * title;
@property(nonatomic,strong)NSArray * items;

@end

@implementation filterBtnView

- (instancetype)initWithTitle:(NSString *)title andItemArrar:(NSArray *)items{
    if (! (self = [super init]))
        return nil;
    self.backgroundColor = [UIColor blackColor];
    self.alpha = 0.6;
    _title = title;
    _items = items;
    [self addSubview:self.titleLabel];
    [self layoutPageFrame];
    [self addSubview:self.selectBtn];
    return self;
}
//点击事件
- (void)selectBtnClick:(UIButton *)sender{
    //多选模式
    //    if (!sender.selected) {
    //        [sender setBackgroundColor:[UIColor brownColor]];
    //    }else{
    //        [sender setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    //    }
    //    sender.selected = !sender.selected;
    //单选模式
    if (_selectBtn == nil){
        sender.selected = YES;
        sender.backgroundColor = [UIColor darkGrayColor];
        _selectBtn = sender;
    } else  if (_selectBtn !=nil &&_selectBtn == sender){
        sender.selected = YES;
        sender.backgroundColor = [UIColor darkGrayColor];
    } else if (_selectBtn!= sender && _selectBtn!=nil){
        _selectBtn.selected = NO;
        _selectBtn.backgroundColor = [UIColor lightGrayColor];
        sender.selected = YES;
        sender.backgroundColor = [UIColor darkGrayColor];
        _selectBtn = sender;
    }
    _selectIndex = sender.tag;
    
    if (self.selectFilterBlock) {
        self.selectFilterBlock(_selectIndex);
    }
   
}
- (void)layoutPageFrame{
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top).offset(10);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(100);
    }];
}
- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:15.0];
        _titleLabel.text = _title;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [UIColor whiteColor];
    }
    return _titleLabel;
}
- (UIButton *)selectBtn{
    if (_selectBtn == nil) {
        if (_items>0) {
            for (int i = 0;  i <_items.count; i ++) {
                _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                [_selectBtn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
                [_selectBtn setTitle:[_items objectAtIndex:i] forState:UIControlStateNormal];
                //单选用到的
                [_selectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [_selectBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
                _selectBtn.backgroundColor = [UIColor lightGrayColor];
                
                //单选用到的
//                [_selectBtn setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
//                [_selectBtn setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
                CGFloat tipDescW = 40.0;
                CGFloat tipDescH = 40.0;
                int totalColumns = 6;//
                CGFloat marginX = (ScreenW-totalColumns*tipDescW)/(totalColumns +1);
//                int row = i/totalColumns;
                int col = i%totalColumns;
                _selectBtn.frame = CGRectMake(marginX + col*(marginX + tipDescW), 50, tipDescW, tipDescH);
                _selectBtn.layer.cornerRadius = 20;
                _selectBtn.tag =i;
                [self addSubview:_selectBtn];
                [_selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
    }
    return _selectBtn;
}

@end
